package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.Impl.ResearchDaoImpl;
import com.ysd.dao.Impl.ScrollDaoImpl;
import com.ysd.entity.Scroll;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminScrollServlet extends BaseServlet {
	/***
	 * 轮番图
	 * localhost:8080/huaduoyoupin/adminScroll?reqName=getScroll
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getScroll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ScrollDaoImpl sDao=new ScrollDaoImpl();
		int page=Integer.parseInt(request.getParameter("page"));
		int size=Integer.parseInt(request.getParameter("size"));
		List<Scroll> slist=sDao.getScrollListPage(page, size);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		
		String res=gson.toJson(slist);
		out.print(res);
		
		out.flush();
		out.close();
	}
    //分页查询
	// localhost:8080/huaduoyoupin/adminScroll?reqName=getAllScroll
	public void getAllScroll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ScrollDaoImpl sDao=new ScrollDaoImpl();
		List<Scroll> slist=sDao.getScrollList();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		
		String res=gson.toJson(slist.size());
		out.print(res);
		
		out.flush();
		out.close();
	}

	//删除轮播图
		 public void delScroll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				int id=Integer.parseInt(request.getParameter("scrollId"));//接收前台传来的参数
				ScrollDaoImpl sDao=new ScrollDaoImpl();//创建实现类
				
				response.setCharacterEncoding("UTF-8");//设置编码集格式
				PrintWriter out=response.getWriter();
				//调用实现类的删除方法，返回受影响行数
				int n=sDao.delScroll(id);
				if(n>0) {
					out.print(Result.toClient(true, "删除成功",""));
				}else {
					out.print(Result.toClient(false, "删除失败",""));
				}
				
				
				out.flush();
				out.close();
			}
	//添加轮播图  localhost:8080/huaduoyoupin/adminScroll?reqName=addScroll
		 public void addScroll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
				ScrollDaoImpl sDao=new ScrollDaoImpl();//创建实现类
				
				response.setCharacterEncoding("UTF-8");//设置编码集格式
				PrintWriter out=response.getWriter();
				String scrollImg=request.getParameter("scrollImg");
				Scroll s=new Scroll(0, scrollImg);
				//调用实现类的删除方法，返回受影响行数
				int n=sDao.addScroll(s);
				if(n>0) {
					out.print(Result.toClient(true, "添加成功",""));
				}else {
					out.print(Result.toClient(false, "添加失败",""));
				}
				
				
				out.flush();
				out.close();
			}
		 
		/**
		 *  localhost:8080/huaduoyoupin/adminScroll?reqName=getScrollById
		 */
		 public void getScrollById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
				ScrollDaoImpl sDao=new ScrollDaoImpl();//创建实现类
				
				response.setCharacterEncoding("UTF-8");//设置编码集格式
				PrintWriter out=response.getWriter();
				int sId=Integer.parseInt(request.getParameter("scrollId"));
				Scroll s=sDao.getScrollById(sId);
				
				Gson gson=new Gson();
				String res=gson.toJson(s);
				out.print(res);
				
				out.flush();
				out.close();
			}
		 /**
		  * localhost:8080/huaduoyoupin/adminScroll?reqName=updateScrollById
		  * 修改轮播图
		  */
		 public void updateScrollById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				ScrollDaoImpl sDao=new ScrollDaoImpl();//创建实现类
				response.setCharacterEncoding("UTF-8");//设置编码集格式
				PrintWriter out=response.getWriter();
				
			    int scrollId=0;
			    if(request.getParameter("scrollId")!=null && !request.getParameter("scrollId").equals("")) {
					scrollId=Integer.parseInt(request.getParameter("scrollId"));
				}
				String scrollImg=request.getParameter("scrollImg");	
				
				Scroll s=new Scroll(scrollId, scrollImg);
				int n=sDao.updateScrollById(s);
				if(n>0) {
					out.print(Result.toClient(true, "修改成功",""));
				}else {
					out.print(Result.toClient(false, "修改失败",""));
				}
				
				
				out.flush();
				out.close();
			}
		 //批量删除
		 //localhost:8080/huaduoyoupin/adminScroll?reqName=delectScrollByIds
		 public void delectScrollByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
				ScrollDaoImpl sDao=new ScrollDaoImpl();//创建实现类
				
				response.setCharacterEncoding("UTF-8");//设置编码集格式
				PrintWriter out=response.getWriter();
				String ids=request.getParameter("ids");
				int n=sDao.delectProductByIds(ids);
				//调用实现类的删除方法，返回受影响行数
				if(n>0) {
					out.print(Result.toClient(true, "删除成功",""));
				}else {
					out.print(Result.toClient(false, "删除失败",""));
				}
				
				
				out.flush();
				out.close();
			}
}
